package com.hyw.supply.controller;

import com.hyw.supply.enums.OrderStatusEnum;
import com.hyw.supply.model.entity.SupplyOrder;
import com.hyw.supply.model.vo.CommonResult;
import com.hyw.supply.model.vo.OrderHomeVO;
import com.hyw.supply.service.SupplyOrderService;
import com.hyw.supply.utils.CustomToken;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 供应商订单信息 前端控制器
 * </p>
 *
 * @author zzp
 * @since 2024-04-18
 */
@RestController
@RequestMapping("/home/page")
@RequiredArgsConstructor
@Tag(name = "首页")
public class HomePageController {


    private final SupplyOrderService supplyOrderService;

    @Operation(summary = "订单管理")
    @GetMapping("/order")
    public CommonResult<OrderHomeVO> getOrderList() {
        OrderHomeVO orderHomeVO = new OrderHomeVO(0, 0, 0, 0);
        List<SupplyOrder> supplyOrderList = supplyOrderService.lambdaQuery()
                .select(SupplyOrder::getOrderStatus, SupplyOrder::getId)
                .eq(SupplyOrder::getCreater, CustomToken.getAccountId())
                .isNotNull(SupplyOrder::getOrderStatus)
                .list();
        if (supplyOrderList.isEmpty()) {
            return CommonResult.success(orderHomeVO);
        }
        Map<Integer, List<SupplyOrder>> orderListMap = supplyOrderList.stream().collect(Collectors.groupingBy(SupplyOrder::getOrderStatus));
        //待发货
        int noShipped = orderListMap.getOrDefault(OrderStatusEnum.shipped_STOCKED.getCode(), List.of()).size()
                + orderListMap.getOrDefault(OrderStatusEnum.SHIPPED_OUTBOUND_FAIL.getCode(), List.of()).size()
                + orderListMap.getOrDefault(OrderStatusEnum.SHIPPED_OUTBOUND_ING.getCode(), List.of()).size()
                + orderListMap.getOrDefault(OrderStatusEnum.SHIPPED_OUTBOUND_PART.getCode(), List.of()).size();
        orderHomeVO.setNoShipped(noShipped);
        //退单待审核
        int pendingReview = orderListMap.getOrDefault(OrderStatusEnum.PENDING_REVIEW.getCode(), List.of()).size();
        orderHomeVO.setPendingReview(pendingReview);
        //已退单
        int charged = orderListMap.getOrDefault(OrderStatusEnum.CHARGED.getCode(), List.of()).size();
        orderHomeVO.setCharged(charged);
        //已发货
        int received = orderListMap.getOrDefault(OrderStatusEnum.RECEIVED.getCode(), List.of()).size();
        orderHomeVO.setReceived(received);
        return CommonResult.success(orderHomeVO);
    }


}
